package com.briup.cms.service.impl;

import com.briup.cms.bean.BaseConfig;
import com.briup.cms.dao.BaseConfigMapper;
import com.briup.cms.dao.extend.BaseConfigExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IBaseConfigService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version 1.0
 * @auther YJ
 * @date 2021/7/18 15:24
 */
@Service

public class BaseConfigServiceImpl implements IBaseConfigService {

    @Autowired
    private BaseConfigMapper mapper;
    @Autowired
    private BaseConfigExtendMapper extendMapper;

  public PageInfo<BaseConfig> findAll(int paramInt1, int paramInt2) {
      PageHelper.startPage(paramInt1, paramInt2, true);
      List<BaseConfig> list = extendMapper.findAll();
      return new PageInfo(list);}
      @Transactional
      public void changeStatus ( int id,int status) throws CustomerException{
          BaseConfig config = mapper.selectByPrimaryKey(Integer.valueOf(id));
          if (config == null) {
              throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_NOT_EXIST);
          }
          this.extendMapper.changeStatus();
          config.setConfigStatus(Integer.valueOf(1));
          mapper.updateByPrimaryKeySelective(config);
      }


      @Override
      public void insert (BaseConfig record){
          mapper.insert(record);
      }
      public void update (BaseConfig record){
          mapper.updateByPrimaryKey(record);
      }
      public void delete ( int id) throws CustomerException {
          BaseConfig config = this.mapper.selectByPrimaryKey(Integer.valueOf(id));
          if (config == null) {
              throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_NOT_EXIST);
          }
          this.mapper.deleteByPrimaryKey(Integer.valueOf(id));
      }
      public BaseConfig ConfigNow()
      {
          List<BaseConfig> baseConfigList = extendMapper.findConfigByStatus(1);
          if ((baseConfigList != null) && (baseConfigList.size() > 0)) {
              return baseConfigList.get(0);
          }
          return new BaseConfig();
      }
  }